<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../layui/css/layui.css"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>个人信息</legend>
</fieldset>
<div class="layui-form-item">
    <div class="layui-inline" >
        <form class="layui-form"  lay-filter="example"  >
            <input type="text" id="id" name="id" style="display: none" class="layui-input">
            <input type="text" id="imgUrl" name="imgurl" style="display: none" class="layui-input">
            <div class="layui-form-item">
                <label class="layui-form-label">头像上传</label>
                <div class="layui-input-inline">
                    <div class="layui-upload-list">
                        <img  id="demo1"  style="width: 40%;height: 40%;margin-right: 50%;border-radius: 50%;" >
                    </div>
                    <button type="button" class="layui-btn" id="test1">上传头像</button>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">登录账号</label>
                <div class="layui-input-inline">
                    <input type="text" id="username" name="username" lay-verify="username"  readonly  placeholder="登录账号" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">昵称</label>
                <div class="layui-input-inline">
                    <input type="text" name="nickname" id="nickname" lay-verify="required"  placeholder="昵称" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">手机</label>
                    <div class="layui-input-inline">
                        <input type="tel" name="phone" id="phone" lay-verify="required|phone" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">邮箱</label>
                    <div class="layui-input-inline">
                        <input type="text" name="email" id="email" lay-verify="email" autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">性别</label>
                <div class="layui-input-block">
                    <input type="radio" name="sex" value="1" title="男" checked="">
                    <input type="radio" name="sex" value="0" title="女">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="button" class="layui-btn" lay-submit lay-filter="userForm">保存修改</button>
                </div>
            </div>
        </form>
    </div>
</div>
<script  src="../../../layui/layui.js" ></script>
<script src="../../../layui/layui.all.js"  ></script>
<script src="../../../js/jquery.min.js"></script>
<script src="../../../js/common.js"></script>
<script>

    var userId = getUrlParam("userId");
    layui.use(['table','upload','element'], function() {
        var form = layui.form;
        var $ = layui.jquery
            ,upload = layui.upload
            ,element = layui.element
            ,layer = layui.layer;

        var imgSrc = window.location.protocol + "//" + window.location.host + "/upload";


        $.ajax({
            type: 'get',
            url: '/sysUser/findById?userId=' + userId,
            async: false,
            success: function (res) {
                console.log(res.data)
                form.val('example', {
                    "id":res.data.id// "name": "value"
                    ,"username": res.data.username // "name": "value"
                    ,"phone": res.data.phone
                    ,"nickname":res.data.nickname
                    ,"email": res.data.email //复选框选中状态
                    ,"sex":res.data.sex
                });
                if(res.data.imgurl!=null){
                    $("#demo1").attr("src",imgSrc+res.data.imgurl)
                }


            }})

        //常规使用 - 普通图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            ,url: '/api/files/uploadImg' //改成您自己的上传接口
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            ,done: function(res){

                //如果上传失败
                if(res.code > 0){
                    return layer.msg('上传失败');
                }else{  console.log(res);

                    $('#imgUrl').val(res.data.url);
                }
                //上传成功的一些操作
                //……
                $('#demoText').html(''); //置空上传失败的状态
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });
                 //监听提交
             form.on('submit(userForm)', function(data){
                 $.ajax({
                     url:"/sysUser/saveUser",
                     method:"post",
                     data:data.field,
                     success:function(data){
                         if(data.code== 404 ){
                             layer.msg(data.msg);
                         }else {
                             layer.msg(data.msg);
                             setTimeout(function(){
                                 layer.close(layer.index);
                                 window.parent.location.reload();
                             },1800 );

                         }
                     }
                 });
             });
         })

</script>
</body>
</html>